package com.lvshou.medicalkit.base;


import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.trello.rxlifecycle3.android.FragmentEvent;
import com.trello.rxlifecycle3.components.RxFragment;

import org.greenrobot.eventbus.EventBus;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import butterknife.ButterKnife;
import io.reactivex.ObservableTransformer;

/**
 * 作者：senon on 2017/12/27 15:59
 * 邮箱：a1083911695@163.com
 * 父类->基类->动态指定类型->泛型设计（通过泛型指定动态类型->由子类指定，父类只需要规定范围即可）
 */
public abstract class BaseFragment<P extends BasePresenter> extends RxFragment implements BaseView {

    /**
     * 引用V层和P层
     */
    private P presenter;

    /**
     * 缓存Fragment view
     */
    private View mRootView;
    public Context mContext;
    public P getPresenter() {
        return presenter;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), container, false);
            ButterKnife.bind(this, mRootView);
            if (presenter == null) {
                presenter = createPresenter();
            }
            if (presenter != null) {
                presenter.attach(this);
            }
            initView();
            initData();
            if (useEventBus()) {
                EventBus.getDefault().register(this);
            }
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    //由子类指定具体类型
    public abstract int getLayoutId();

    public abstract P createPresenter();

    public abstract void initView();

    public abstract void initData();

    @Override
    public <T> ObservableTransformer<T, T> bindLifecycle() {
        return this.bindUntilEvent(FragmentEvent.DETACH);
//        return this.bindToLifecycle();//绑定activity，与activity生命周期一样
    }

    /**
     * 是否使用eventBus
     *
     * @return
     */
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (useEventBus()) {
            if (EventBus.getDefault().isRegistered(this)) {
                //注销eventBus
                EventBus.getDefault().unregister(this);
            }
        }
        if (presenter != null) {
            presenter.detach();
        }
    }

    /**
     * 初始化 Toolbar
     *
     * @param toolbar
     */
    protected void initToolBar(Toolbar toolbar) {
        initToolBar(toolbar, false, false, "");
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, boolean showTitleEnabled, int resTitle) {
        initToolBar(toolbar, homeAsUpEnabled, showTitleEnabled, resTitle != 0 ? getString(resTitle) : "");
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, boolean showTitleEnabled, String title) {
        if (!TextUtils.isEmpty(title)) {
            toolbar.setTitle(title);
        }
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        ((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
        ((BaseActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(showTitleEnabled);
    }

    protected void setToolBarTextView(@NonNull TextView toolbarTextView, @NonNull String text) {
        toolbarTextView.setText(text);
        setVisibility(toolbarTextView, true);
    }

    protected void setToolBarTextView(@NonNull TextView toolbarTextView, @StringRes int stringId) {
        toolbarTextView.setText(stringId);
        setVisibility(toolbarTextView, true);
    }

    protected void setToolBarImageView(@NonNull ImageView toolbarImageView, @DrawableRes int resId) {
        toolbarImageView.setImageResource(resId);
        setVisibility(toolbarImageView, true);
    }

    protected void setVisibility(@NonNull View view, boolean isVisible) {
        view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }
}